import java.util.*;
import java.sql.*;

public class SectorFileToTable {

    private String fileName    = null;
    private String tableName   = null;

    public static void main (String args[]) {
	String file_name = "Sector.txt";
	String table_name = "Sector";
	String query_string = null;
	String data_dir = "./Data/Sector/";

	SectorFileToTable ft = new SectorFileToTable ();
	ft.setFileName (data_dir+file_name);
	ft.setTableName (table_name);
	ft.loadSectorFileToTable();
    }

    public void loadSectorFileToTable () {

	Hashtable s_hashtable = new Hashtable ();
	String query_string = null;
	String s_id = null;
	String s_value = null;
	int int_s_id = 0;

	SectorFileLoader fl = new SectorFileLoader ();

	fl.setFileName(fileName);
	fl.loadFile();
	s_hashtable = fl.getHashTable();

	Enumeration enum = s_hashtable.keys();  
	while (enum.hasMoreElements()) {
	    s_id     = (String)enum.nextElement();
	    int_s_id = Integer.parseInt(s_id);
	    s_value  = ((String)s_hashtable.get(s_id)).replaceAll(" ","");

	    query_string = "INSERT INTO " + tableName +" VALUES ("+"'"+s_value +"'," + int_s_id +");";
	    insertIntoDatabase (query_string); 
	}
    }

    private void insertIntoDatabase (String query_string) {
 
	DataBaseInsert dbInsert = new DataBaseInsert();
	dbInsert.setQueryString(query_string);
	dbInsert.insertData();
    }

    public String getHashtableValueByKey(Hashtable ht, String key )
    {
	String temp = null;
	String value = null;

	Enumeration enum = ht.keys();  
	while (enum.hasMoreElements()) {
	    temp  = (String)enum.nextElement();
	    if (temp.equals(key)) {
		value  = (String)ht.get(temp);
	    }
	}

	return value;
    }
    
    public void setTableName( String table_name )
    {
	tableName = table_name;
    }

    public void setFileName( String file_name )
    {
	fileName = file_name;
    }
}

